"""
Case Type   : resource pool
Case Name   : 创建业务资源池后，删除关联的workload控制组
Create At   : 2022/08/29
Owner       : songjing20
Description :
    1.预置子class控制组及Workload控制组
    2.创建组资源池和业务资源池
    3.删除业务控制组workload_a1
    4.查询组和业务资源池信息
    5.删除子class控制组calss_b
    6.查询组和业务资源池信息
    7.环境清理
Expect      :
    1.创建成功
    2.创建成功
    3.创建成功
    4.查询成功 ERROR:  cgroup is not initialized or group name is invalid
    5.成功
    6.成功 ERROR:  cgroup is not initialized or group name is invalid
    7.清理成功
History     :
"""
import os
import unittest

from yat.test import Node
from yat.test import macro
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant


class ResourcePool(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f"-----{os.path.basename(__file__)} start-----")
        self.pri_node = Node('PrimaryDbUser')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.class_a = 'class_a'
        self.class_b = 'class_b'
        self.group_a = 'workload_a1'
        self.group_b = 'workload_b1'
        self.respool = ['resp_resource_pool_0063_1',
                        'resp_resource_pool_0063_2',
                        'resp_resource_pool_0063_3',
                        'resp_resource_pool_0063_4']

    def test_resource_pool(self):
        text = "-----step1:预置子class控制组及Workload控制组;expect:创建成功-----"
        self.log.info(text)
        cmd = f"source {macro.DB_ENV_PATH};" \
              f"gs_cgroup -c -S {self.class_a} -s 20; " \
              f"gs_cgroup -c -S {self.class_a} -G {self.group_a} -g 20; " \
              f"gs_cgroup -c -S {self.class_b} -s 10; " \
              f"gs_cgroup -c -S {self.class_b} -G {self.group_b} -g 60; " \
              f"gs_cgroup -p"
        self.log.info(cmd)
        cmd_res = self.pri_node.sh(cmd).result()
        self.log.info(cmd_res)
        self.assertIn(f"CLASS  Name: {self.class_a}", cmd_res,
                      f'执行失败:{text}')
        self.assertIn(f"CLASS  Name: {self.class_b}", cmd_res,
                      f'执行失败:{text}')
        self.assertIn(f"DEFWD  Name: {self.group_a}", cmd_res,
                      f'执行失败:{text}')
        self.assertIn(f"DEFWD  Name: {self.group_b}", cmd_res,
                      f'执行失败:{text}')

        text = "-----step2:创建组资源池和业务资源池;expect:创建成功-----"
        self.log.info(text)
        sql_res = self.pri_sh.execut_db_sql(
            f"""create resource pool {self.respool[0]} 
            with(control_group = '{self.class_a}');
            create resource pool {self.respool[1]} 
            with(control_group = '{self.class_a}:{self.group_a}');
            create resource pool {self.respool[2]} 
            with(control_group = '{self.class_b}');
            create resource pool {self.respool[3]} 
            with(control_group = '{self.class_b}:{self.group_b}');""")
        self.log.info(sql_res)
        self.assertEqual(4, sql_res.count(
            (self.constant.create_resource_pool_success)), f'执行失败:{text}')

        text = "-----step3:删除业务控制组workload_a1;expect:成功-----"
        self.log.info(text)
        cmd = f"source {macro.DB_ENV_PATH};" \
              f"gs_cgroup -d -S {self.class_a} -G {self.group_a}; " \
              f"gs_cgroup -p"
        self.log.info(cmd)
        cmd_res = self.pri_node.sh(cmd).result()
        self.log.info(cmd_res)
        self.assertNotIn(f"DEFWD  Name: {self.group_a}", cmd_res,
                         f'执行失败:{text}')

        text = "-----step4:查询组和业务资源池信息;expect:成功-----"
        self.log.info(text)
        query_sql = """select * from pg_resource_pool;
        select * from gs_control_group_info('{}');
        select * from gs_control_group_info('{}');"""
        sql_res = self.pri_sh.execut_db_sql(
            query_sql.format(self.respool[0], self.respool[1]))
        self.log.info(sql_res)
        flag = 'ERROR:  cgroup is not initialized or group name {} is invalid'
        self.assertIn(f'{self.respool[1]} |          20 |           -1 | '
                      f'{self.class_a}:{self.group_a}', sql_res,
                      f'执行失败:{text}')
        self.assertTrue(flag.format(self.class_a) in sql_res and flag.format(
            f'{self.class_a}:{self.group_a}') in sql_res, '执行失败:{text}')

        text = "-------step5:删除子class控制组calss_b;expect:成功-----"
        self.log.info(text)
        cmd = f"source {macro.DB_ENV_PATH};" \
              f"gs_cgroup -d -S {self.class_b}; " \
              f"gs_cgroup -p"
        self.log.info(cmd)
        cmd_res = self.pri_node.sh(cmd).result()
        self.log.info(cmd_res)
        self.assertNotIn(f"CLASS  Name: {self.class_b}", cmd_res,
                         f'执行失败:{text}')
        self.assertNotIn(f"DEFWD  Name: {self.group_b}", cmd_res,
                         f'执行失败:{text}')

        text = "-----step6:查询组和业务资源池信息;expect:成功-----"
        self.log.info(text)
        sql_res = self.pri_sh.execut_db_sql(
            query_sql.format(self.respool[2], self.respool[3]))
        self.log.info(sql_res)
        self.assertIn(f'{self.respool[2]} |          20 |           -1 | '
                      f'{self.class_b}             |                10',
                      sql_res, f'执行失败:{text}')
        self.assertIn(f'{self.respool[3]} |          20 |           -1 | '
                      f'{self.class_b}:{self.group_b}', sql_res,
                      f'执行失败:{text}')
        self.assertTrue(flag.format(self.class_b) in sql_res and flag.format(
            f'{self.class_b}:{self.group_b}') in sql_res, '执行失败:{text}')

    def tearDown(self):
        text = "-----step7:环境清理;expect:成功-----"
        self.log.info(text)
        drop_res = self.pri_sh.execut_db_sql(
            f"drop resource pool if exists {self.respool[0]};"
            f"drop resource pool if exists {self.respool[2]};")
        self.log.info(drop_res)
        cmd = f"source {macro.DB_ENV_PATH};gs_cgroup --revert"
        self.log.info(cmd)
        cmd_res = self.pri_node.sh(cmd).result()
        self.assertIn(self.constant.drop_resource_pool_success, drop_res,
                      f'执行失败:{text}')
        self.assertEqual(drop_res.count('NOTICE:  all child resource pool of'),
                         2, f'执行失败:{text}')
        self.assertEqual('', cmd_res, f'执行失败:{text}')
        self.log.info(f"-----{os.path.basename(__file__)} end-----")
